package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.AboutMessage;
import com.shbaiche.bifenba.model.user.UserCommentMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.ui.base.WebActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroid.event.BusFactory;
import cn.droidlover.xdroidbase.router.Router;
import okhttp3.Call;


/**
 * Created by wanglei on 2016/12/9.
 */

public class FeedbackActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.feed_edt_phone)
    EditText phone;
    @BindView(R.id.feed_edt)
    EditText edt;
    Dialog dg;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void setListener() {

    }

    @OnClick({
            R.id.feedback_btn_submit
    })
    public void clickEvent(View view) {
        switch (view.getId()) {

            case R.id.feedback_btn_submit:
                if (TextUtils.isEmpty(edt.getText().toString())) {
                    ToastUtil.show("反馈内容不能为空!");
                    return;
                }
                String conte = edt.getText().toString();
                String p = phone.getText().toString() + "";

                UserCommentMessage message = new UserCommentMessage();
                message.setContent(conte);
                message.setContact(p);

                dg = new Dialog(this);
                dg.setContentView(R.layout.view_loading);
                dg.show();

                NetApi.UserComment(message, new JsonCallback<TransMessage>() {

                    @Override
                    public void onFail(Call call, Exception e, int id) {
                        dg.hide();
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.show("onFail" + e.getMessage());
                        dg.hide();
                    }

                    @Override
                    public void onResponse(TransMessage response, int id) {
                        dg.hide();
                        Gson gson = new Gson();
                        AboutMessage msg = gson.fromJson(response.getMessage(), AboutMessage.class);

                        if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                            ToastUtil.show("感谢您的反馈！");
                            finish();
                        } else {
                        }
                    }
                });
                break;
        }
    }

    public static void launch(Activity activity) {
        Router.newIntent()
                .from(activity)
                .to(FeedbackActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void setToolbar() {
        initToolbar(toolbar, "意见反馈");
    }
}
